﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BotonJXC.BLL;
using BotonJXC.Entity;
using BotonJXC.Util;
using ComponentFactory.Krypton.Toolkit;
using NHibernate;
using BotonJXC.DAO;

namespace BotonJXC.Forms.Controls
{
    public partial class SupplierControl : BaseInfoControl
    {
        SupplierBLL _supplierBll = new SupplierBLL();

        string _supplierName;
        string _supplierCode;
        string _mnCode;

        public SupplierControl()
        {
            InitializeComponent();
        }

        public override IList<object> SelectedItems
        {
            get
            {
                IList<object> selectedItems = new List<object>();
                foreach (DataGridViewRow r in kdgvSupplier.SelectedRows)
                {
                    selectedItems.Add(r.DataBoundItem);
                }
                return selectedItems;
            }
        }

        public override void AddInfo()
        {

        }
        public override void SaveInfo()
        {
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvSupplier.Rows)
                {
                    Supplier supplier = r.DataBoundItem as Supplier;
                    if (supplier == null)
                        continue;
                    if (supplier.SupplierId == 0)
                    {
                        _supplierBll.Save(supplier);
                    }
                    else if ("1".Equals(r.Tag) && supplier.SupplierId != 0)
                    {
                        _supplierBll.Update(supplier);
                    }
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
        }
        public override void DeleteInfo()
        {
            IList<Supplier> suppliers = (IList<Supplier>)kdgvSupplier.DataSource;
            ITransaction trans = HBHelper.BeginTransaction();
            try
            {
                foreach (DataGridViewRow r in kdgvSupplier.SelectedRows)
                {
                    Supplier supplier = r.DataBoundItem as Supplier;
                    if (supplier != null)
                    {
                        _supplierBll.Delete(supplier.SupplierId);
                    }
                    suppliers.RemoveAt(r.Index);
                }
                trans.Commit();
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
            }
            finally
            {
                HBHelper.Close();
            }
            kdgvSupplier.Refresh();
        }

        private void kdgvSupplier_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
                kdgvSupplier.Rows[e.RowIndex].Tag = "1";
        }

        private void kdgvSupplier_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            Console.WriteLine(kdgvSupplier.Columns[e.ColumnIndex].Name);
            if (kdgvSupplier.Columns[e.ColumnIndex].Name == "kdgvcbcEnabled")
            {
                IList<Supplier> suppliers = (IList<Supplier>)kdgvSupplier.DataSource;
                if(e.RowIndex < suppliers.Count)
                    e.Value = suppliers[e.RowIndex].Enabled == "Y" ? "是" : "否";
            }
        }

        private void kdgvSupplier_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (kdgvSupplier.Columns[kdgvSupplier.CurrentCell.ColumnIndex].Name == "kdgvcbcEnabled")
            {
                KryptonDataGridViewComboBoxEditingControl cbc = e.Control as KryptonDataGridViewComboBoxEditingControl;
                cbc.DropDownClosed += new EventHandler(cbc_DropDownClosed);
            }
        }

        void cbc_DropDownClosed(object sender, EventArgs e)
        {
            KryptonDataGridViewComboBoxEditingControl cbc = sender as KryptonDataGridViewComboBoxEditingControl;
            IList<Supplier> storeDatas = (IList<Supplier>)kdgvSupplier.DataSource;
            storeDatas[kdgvSupplier.CurrentCell.RowIndex].Enabled = cbc.SelectedIndex == 0 ? "Y":"N";
        }

        private void SupplierControl_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                LoadData();
            }
        }

        void LoadData()
        {
            PageModel<Supplier> pageModel = _supplierBll.Find(_supplierName, _supplierCode, _mnCode, pager1.PageStart, pager1.PageSize);
            pager1.TotalCount = pageModel.TotalCount;
            kdgvSupplier.DataSource = new BindingList<Supplier>(pageModel.Results);
        }

        private void kbQuery_Click(object sender, EventArgs e)
        {
            _supplierName = ktbSupplierName.Text.Trim();
            _supplierCode = ktbSupplierCode.Text.Trim();
            _mnCode = ktbMnCode.Text.Trim();
            LoadData();
        }

        private void pager1_PageChanged(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
